import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import VCharts from 'v-charts'
import App from './App.vue'
import router from './router'
import store from './store'
import directives from './js/directives'
import filters from './js/filters'

Vue.use(ElementUI, { size: 'small', zIndex: 3000 })
Vue.use(VCharts)
Vue.config.productionTip = false
Vue.use(directives)
Vue.use(filters)

router.beforeEach((to, from, next) => {
    let token = localStorage.getItem('token')
    // 判断页面是否需要登录权限
    if (to.meta.requireAuth) {
        if (token || to.path === '/login') {
            next()
        } else {
            ElementUI.Message.error('登录过期，请重新登录')
            next({
                path: '/login'
            })
        }
    } else {
        next()
    }
    // 如果本地存在token,则不允许直接跳转到登录页面
    if (to.fullPath === '/login') {
        if (token) {
            next({
                path: from.fullPath
            })
        } else {
            next()
        }
    }
})

new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')
